import json

from crawler_core.config.config_redis import RedisClient
from crawler_core.utils.sync.redis_sync import RedisSync


class RedisUtils(object):
    '''
   **********************************
   *****    event 任务调度数据   ****
   *****    login 服务处理数据   ****
   **********************************
   '''

    # 任务队列
    @staticmethod
    def getEventTask(taskId):
        key = "event:task:" + taskId
        value = RedisClient.get(key)
        return RedisUtils.getJson(value)

    @staticmethod
    def setEventTask(taskId, context):
        key = "event:task:" + taskId
        value = RedisUtils.getJsonStr(context)
        return RedisClient.set(key, value)

    @staticmethod
    def refreshEventTask(taskId, context):
        key = "event:task:" + taskId
        value = RedisUtils.getJsonStr(context)
        return RedisClient.set(key, value)

    @staticmethod
    def delEventTask(taskId):
        key = "event:task:" + taskId
        RedisClient.delete(key)

    # 任务中 accountList 记录账号使用情况,cookies失效, 账号密码错误
    @staticmethod
    def getEventAccount(corp_code, _type):
        name = "event:account"
        key = corp_code + "@#@" + _type
        value = RedisClient.hget(name, key)
        if value:
            obj = RedisUtils.getJson(value)
            return obj
        return None

    @staticmethod
    def setEventAccount(corp_code, _type, value):
        name = "event:account"
        key = corp_code + "@#@" + _type
        RedisClient.hset(name, key, value)

    @staticmethod
    def addEventAccount(corp_code, _type, username, status):
        RedisSync().addEventAccount(corp_code, _type, username, status)

    '''
    **********************************
    *****    登录数据             ****
    **********************************
    '''

    # 登录任务
    @staticmethod
    def setLoginTask(taskId):
        key = "login:task"
        RedisClient.rpush(key, taskId)

    @staticmethod
    def getLoginTask():
        key = "login:task"
        return RedisClient.lpop(key)

    @staticmethod
    def getLoginReturn(taskId):
        key = "login:return:" + taskId
        value = RedisClient.get(key)
        if value:
            return RedisUtils.getJson(value)
        else:
            return None

    @staticmethod
    def setLoginReturn(taskId, content):
        key = "login:return:" + taskId
        value = RedisUtils.getJsonStr(content)
        RedisClient.set(key, value)

    @staticmethod
    def setLoginDtemp(type, corpCode, account):
        name = "login:dtemp"
        key = type + "@#@" + corpCode + "@#@" + account
        value = "1"
        RedisClient.hset(name, key, value)

    @staticmethod
    def isExistLoginDtemp(type, corpCode, account):
        name = "login:dtemp"
        key = type + "@#@" + corpCode + "@#@" + account
        return RedisClient.hexists(name, key)

    @staticmethod
    def clearLoginDTemp(type, corpCode, account):
        name = "login:dtemp"
        key = type + "@#@" + corpCode + "@#@" + account
        RedisClient.hdel(name, key)

    # 登录 cookies 池
    @staticmethod
    def getCookies(type, corpCode, account):
        key = "cookies:" + type + "@#@" + corpCode + "@#@" + account
        value = RedisClient.get(key)
        if not value:
            return None
        return RedisUtils.getJson(value)

    @staticmethod
    def setCookies(type, corpCode, account, object):
        key = "cookies:" + type + "@#@" + corpCode + "@#@" + account
        value = RedisUtils.getJsonStr(object)
        RedisClient.set(key, value)

    '''
    **********************************
    *****    下载数据             ****
    **********************************
    '''

    # 生产下载任务, 重复过滤锁
    @staticmethod
    def addEventDownloadLock(key, task_id):
        name = "event:download:lock"
        RedisClient.hset(name, key, task_id)

    @staticmethod
    def existEventDownloadLock(key):
        name = "event:download:lock"
        return RedisClient.hexists(name, key)

    @staticmethod
    def removeEventDownloadLock(*key):
        name = "event:download:lock"
        RedisClient.hdel(name, key)

    # 下载任务
    @staticmethod
    def setDownloadTask(taskId):
        key = "download:task"
        RedisClient.rpush(key, taskId)

    @staticmethod
    def getDownloadTask():
        key = "download:task"
        return RedisClient.lpop(key)

    # 下载任务解析数据
    @staticmethod
    def getCacheDownloadData(task_id):
        key = "cache:download:" + task_id
        value = RedisClient.get(key)
        return RedisUtils.getJson(value)

    @staticmethod
    def setCacheDownloadData(task_id, data):
        key = "cache:download:" + task_id
        value = RedisUtils.getJsonStr(data)
        RedisClient.set(key, value)

    @staticmethod
    def removeCacheDownloadData(task_id):
        key = "cache:download:" + task_id
        RedisClient.delete(key)

    # 下载 异常 api 服务可直接使用
    @staticmethod
    def setApiDownloadReturn(task_id, data):
        key = "api:download:return:" + task_id
        value = RedisUtils.getJsonStr(data)
        RedisClient.set(key, value)

    @staticmethod
    def getApiDownloadReturn(task_id):
        key = "api:download:return:" + task_id
        value = RedisClient.get(key)
        if value:
            return RedisUtils.getJson(value)
        else:
            return None

    @staticmethod
    def removeApiDownloadReturn(task_id):
        key = "api:download:return:" + task_id
        RedisClient.delete(key)

    '''
    **********************************
    *****    查询数据             ****
    **********************************
    '''

    # 查询,生产者一级处理, event任务数据对象(root数据),
    @staticmethod
    def getEventSearchContext(task_id):
        key = "event:search:context:" + task_id
        value = RedisClient.get(key)
        return RedisUtils.getJson(value)

    @staticmethod
    def setEventSearchContext(task_id, context):
        key = "event:search:context:" + task_id
        value = RedisUtils.getJsonStr(context)
        return RedisClient.set(key, value)

    @staticmethod
    def refreshEventSearchContextIsOver(task_id, is_over):
        RedisSync().refreshEventSearchContextIsOver(task_id, is_over)

    @staticmethod
    def refreshEventSearchContext(task_id, size):
        return RedisSync().refreshEventSearchContextCardNumber(task_id, size)

    # 生产者二级分片处理, event任务队列,
    @staticmethod
    def setEventSearchSecondTask(task_id):
        key = "event:search_second:task"
        return RedisClient.rpush(key, task_id)

    @staticmethod
    def getEventSearchSecondTask():
        key = "event:search_second:task"
        return RedisClient.lpop(key)

    # 查询 任务 队列
    @staticmethod
    def getSearchTask():
        key = "search:task"
        return RedisClient.lpop(key)

    @staticmethod
    def getSearchTaskRight():
        key = "search:task"
        return RedisClient.rpop(key)

    @staticmethod
    def setSearchTask(task_id):
        key = "search:task"
        return RedisClient.rpush(key, task_id)

    # 查询任务 解析数据
    @staticmethod
    def getCacheSearchData(task_id):
        key = "cache:search:" + task_id
        value = RedisClient.get(key)
        return RedisUtils.getJson(value)

    @staticmethod
    def setCacheSearchData(task_id, data):
        key = "cache:search:" + task_id
        value = RedisUtils.getJsonStr(data)
        RedisClient.set(key, value)

    @staticmethod
    def removeCacheSearchData(task_id):
        key = "cache:search:" + task_id
        RedisClient.delete(key)

    # 异常 api 数据,直接返回使用
    @staticmethod
    def setApiSearchReturn(task_id, data):
        key = "api:search:return:" + task_id
        value = RedisUtils.getJsonStr(data)
        RedisClient.set(key, value)

    @staticmethod
    def getApiSearchReturn(task_id):
        key = "api:search:return:" + task_id
        value = RedisClient.get(key)
        if value:
            return RedisUtils.getJson(value)
        else:
            return None

    @staticmethod
    def removeApiSearchReturn(task_id):
        key = "api:search:return:" + task_id
        value = RedisClient.delete(key)

    '''
    **********************************
    *****     API服务             ****
    **********************************
    '''

    # 任务队列
    @staticmethod
    def setApiTask(taskId):
        key = "api:task"
        RedisClient.rpush(key, taskId)

    @staticmethod
    def getApiTask():
        key = "api:task"
        return RedisClient.lpop(key)

    @staticmethod
    def getJsonStr(data):
        return json.dumps(data)

    @staticmethod
    def getJson(str):
        return json.loads(str)

    '''
    **********************************
    *****    重复数据过滤         ****
    **********************************
    '''
    @staticmethod
    def setKey(task_id,value = 'xxx'):
        name = "key:task"
        return RedisClient.hset(name, task_id,value)

    @staticmethod
    def getKeyAll(name = "key:task"):
        return RedisClient.hgetall(name)
